const bodyParser = require('body-parser')
const userList = [{
  username: 'admin',
  password: 'admin'
}, {
  username: 'wwf',
  password: 'wwf'
}, {
  username: 'ph',
  password: 'ph'
}]

module.exports = app => {
  app.use(bodyParser.json())

  app.post('/api/login', (req, res) => {
    const { username, password } = req.body;
    const user = userList.find(v => v.username === username && v.password === password);
    if(user) {
      res.send({
        code: 200,
        message: '登陆成功',
        data: {
          ...user
        }
      })
    }
    else {
      res.send({
        code: 400,
        message: '账户密码错误， 请重新登陆'
      })
    }
  })

  app.post('/api/regist', (req, res) => {
    const { username, password } = req.body;
    
    const user = userList.find(v => v.username === username)

    if(user) {
      // 账号存在
      res.send({
        code: 400,
        message: '账号存在， 请重新注册'
      })
    } else {
      // 账号不存在
      userList.push({
        username, password
      });
      res.send({
        code: 200,
        message: '注册成功'
      })
    }
  })

}